<template>
    <div class="login">
        <div style="text-align: center;width: 500px; margin: 50px auto; padding: 30px 50px;">
            <h1  style="text-align: center; margin: 20px 0;color: #505458">登录页面</h1>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名:" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码:" prop="password">
                    <el-input type="password" v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item style="text-align: center">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
            <div style="text-align: center;margin: auto">
                <el-link href="/Email">邮箱登录</el-link><br>
                <el-link href="/Register">注册</el-link>
            </div>
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            ruleForm: {
                username: '',
                password: '',
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 9, message: '长度在 2 到 9 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 2, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // alert('您输入的用户名和密码的格式是正确的，但是，登录功能我们还没做！');
                    let url ='http://localhost:9080/users/login';
                    console.log('尝试登录.....');
                    console.log('请求路径为:'+url);
                    console.log('请求参数为:'+this.ruleForm);
                    console.log(this.ruleForm)
                    let formData = this.qs.stringify(this.ruleForm);
                    console.log('将ruleForm对象转换为FormData:')
                    console.log(formData);
                    this.axios.post(url, formData).then((response)=>{
                        console.log('服务器端响应的结果：' + response);
                        console.log(response);
                        let responserDate = response.data;
                        if (responserDate.state == 20000){
                            console.log('登录成功');
                            this.$message({
                                message: '登录成功',
                                type: 'success'
                            });
                        }else if (responserDate.state == 40400){
                            console.log('登录失败，用户名错误!');
                            this.$notify.error({
                                title: '提示',
                                message:  responserDate.message,

                            });
                        }else {
                            console.log('登录失败，密码错误!');
                            this.$notify.error({
                                title: '提示',
                                message: responserDate.message,

                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>
<style>


</style>
